package leetcode;

public class LC46 {
    public static void main(String[] args) {
        LC46 lc46 = new LC46();
        lc46.buildTree(new int[]{3,9,20,15,7},new int[]{9,3,15,20,7});
    }

    public TreeNode buildTree(int[] preorder, int[] inorder) {
        return buildTree(preorder,inorder, Integer.MAX_VALUE);
    }

    int pre = 0;
    int in = 0;
    public TreeNode buildTree(int[] preorder, int[] inorder, long stop) {
        if(pre == preorder.length) {
            return null;
        }

        if(inorder[in] == stop){
            in++;
            return null;
        }

        int val = preorder[pre];
        pre++;
        TreeNode root = new TreeNode(val);
        root.left = buildTree(preorder, inorder, val);
        root.right = buildTree(preorder, inorder, stop);
        return root;
    }
}
